package com.gopay.bis.service.bank.impl.ebank;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.BosB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BosConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bos.ebank.BosB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
/**
 * 上海网银B2C业务实现
 * 
 * @BosB2cBankServiceImpl.java
 * @author LiLiLi
 */
public class BosB2cBankServiceImpl implements BankInteractRemoteService {
	
	@Resource(name = "bosB2cManager")
    private BosB2cManager bosB2cManager;
	
	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
    	try{
    		BosB2cMessage messageB2C = new BosB2cMessage();
    		BeanUtils.copyProperties(base, messageB2C);
            messageB2C = (BosB2cMessage)bosB2cManager.sign(messageB2C);
            result.setDefaultModel(messageB2C);
            result.setSuccess(true);
    	}catch(GopayException e){
    		BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
        }
        return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		 Result result = new Result(false);
	    	try{
	            boolean flag = false;
	    		BosB2cMessage messageB2C = new BosB2cMessage();
	    		BeanUtils.copyProperties(base, messageB2C);	            
	            flag = (Boolean)bosB2cManager.verify(messageB2C);
	            result.setDefaultModel(flag);
	            result.setModel("message", messageB2C);
	            result.setSuccess(true);

            	Map<String, String> param = new HashMap<String, String>();
            	// 通知后台地址入账
            	if(messageB2C.getTranResult().equals(BosConstants.ORDER_STATUS_SUCCESS)){
	                param.put("orderId", messageB2C.getOrderId());
	                param.put("amt", messageB2C.getAmount().toString());
	                param.put("cardType", "Unknown");
	                param.put("notifyNum", "1");
	                param.put("payStatus", "00");
	                param.put("retCode", "00");
	                param.put("retMsg", "验签成功");
	                param.put("signData", messageB2C.getSignMessage());
	                param.put("isFront", String.valueOf(base.isFront()));
	                result.setModel("isNotifyCashier", param);
					result.setOrderStatus(true);
	            }else{
	                // 通知后台地址该订单失败
	                param.put("orderId", messageB2C.getOrderId());
	                param.put("amt", messageB2C.getAmount().toString());
	                param.put("cardType", "Unknown");
	                param.put("notifyNum", "1");
	                param.put("payStatus", "01");
	                param.put("retCode", "01");
	                param.put("retMsg", "验签失败");
	                param.put("signData", messageB2C.getSignMessage());
	                param.put("isFront", String.valueOf(base.isFront()));
	                result.setModel("isNotifyCashier", param);
					result.setOrderStatus(false);
	        	}
            
	    	}catch(GopayException e){
	    		BankLogUtils.printServiceImplExceptionLog(e);
	    		result.setResultCode(e.getErrCode()); 		
	    	} catch (Exception ex) {
				BankLogUtils.printServiceImplExceptionLog(ex);
				result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
			}
	        return result;
	}

	@Override
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		try {
    		BosB2cMessage messageB2C = new BosB2cMessage();
    		BeanUtils.copyProperties(base, messageB2C);
			BosB2cMessage res = (BosB2cMessage)bosB2cManager.query(messageB2C);
			result.setDefaultModel(res);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())){
				Map<String, String> param = new HashMap<String, String>();
				param.put("orderId", messageB2C.getOrderId());
				param.put("amount", messageB2C.getAmount().toString());
				if (res == null) {
					// 查询订单为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				String returnCode = res.getRetCode(); // 订单查询交易返回码
				String retMsg = res.getErrMsg(); // 订单查询交易信息
				if (returnCode != null && !returnCode.equals(BosConstants.ORDER_RET_CODE_SUCCESS)) {
					// 查询订单错误
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", retMsg);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				String orderStatus = res.getOrdersResults();// 订单状态
				//成功处理
				if (orderStatus != null && !orderStatus.equals("")) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					if (orderStatus.equals(BosConstants.ORDER_STATUS_SUCCESS)){
						param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
						param.put("returnMessage", "交易成功");
					}
					//失败处理
					else if (orderStatus.equals(BosConstants.ORDER_STATUS_FAILURE)) {
						param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
						param.put("returnMessage", "交易失败");
					}
					//不处理
					else if (orderStatus.equals(BosConstants.ORDER_STATUS_DEALING)) {
						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", "银行返回交易处理中");
					} else if (orderStatus.equals(BosConstants.ORDER_STATUS_SUSPICIOUS)) {
						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", "银行返回支付交易可疑");
					} else {
						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", "银行返回交易未明情况");
					}
				}// 没有查到订单
				else {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "银行返回订单未查到");
				}
				result.setModel("queryResult", param);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		}
		return result;
	}

	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
